package guda.house.web.action.sys.admin;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.AdminBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.AdminEditForm;
import guda.house.biz.form.AdminForm;
import guda.house.biz.query.AdminQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.AdminDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 15/11/7.
 */
@Controller
public class AdminAction {

    @Autowired
    private AdminBiz adminBiz;

    @RequestMapping(value = "sys/admin/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        AdminQuery adminQuery = new AdminQuery();
        adminQuery.setPageNo(pageId);
        adminQuery.setPageSize(pageSize);
        modelMap.put("query",adminQuery);
        BizResult bizResult = adminBiz.list(adminQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/admin/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }



    @RequestMapping(value = "sys/admin/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, AdminEditForm adminEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = adminBiz.queryForEdit(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            adminEditForm.initForm(((AdminDO) bizResult.data.get("adminDO")));
            String[] array = (String[])bizResult.data.get("roleIdArray");
            adminEditForm.setRoleIdArray(array);
            return "sys/admin/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/admin/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = adminBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/admin/detail.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/admin/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, AdminForm adminForm,
                         BindingResult result, Map<String,Object> model) {
        BizResult bizResult = adminBiz.queryForCreate();
        modelMap.putAll(bizResult.data);
        return "sys/admin/create.vm";
    }

    @RequestMapping(value = "sys/admin/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid AdminForm adminForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            BizResult bizResult = adminBiz.queryForCreate();
            modelMap.putAll(bizResult.data);
            return "sys/admin/create.vm";
        }
        BizResult bizResult = adminBiz.create(adminForm);
        if (bizResult.success) {
            return "redirect:/sys/admin/list.htm";
        } else {
            result.rejectValue("loginName",bizResult.code);
            bizResult = adminBiz.queryForCreate();
            modelMap.putAll(bizResult.data);
            return "sys/admin/create.vm";
        }

    }

    @RequestMapping(value = "sys/admin/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid AdminEditForm adminEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            BizResult bizResult = adminBiz.queryForCreate();
            modelMap.putAll(bizResult.data);
            return "sys/admin/edit.vm";
        }
        BizResult bizResult = adminBiz.update(adminEditForm);
        if (bizResult.success) {
            return "redirect:/sys/admin/list.htm";
        } else {
            result.rejectValue("loginName",bizResult.code);
            bizResult = adminBiz.queryForCreate();
            modelMap.putAll(bizResult.data);
            return "sys/admin/edit.vm";
        }

    }

    @RequestMapping(value = "sys/admin/doDelete.htm")
    public void doDelete(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = adminBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(),response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
